// Copyright tang.  All rights reserved.
// https://gitee.com/inrgihc/dbswitch
//
// Use of this source code is governed by a BSD-style license
//
// Author: tang (inrgihc@126.com)
// Date : 2020/1/2
// Location: beijing , china
/////////////////////////////////////////////////////////////
package middle.ground.dao;

import middle.ground.entity.AssignmentJobEntity;
import middle.ground.mapper.AssignmentJobMapper;
import middle.ground.model.ops.OpsTaskJobTrend;
import middle.ground.type.JobStatusEnum;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Repository
public class AssignmentJobDAO {

	@Resource
	private AssignmentJobMapper assignmentJobMapper;

	public AssignmentJobEntity newAssignmentJob(Long assignmentId, Integer scheduleMode,
												String jobKey) {
		AssignmentJobEntity assignmentJobEntity = new AssignmentJobEntity();
		assignmentJobEntity.setAssignmentId(assignmentId);
		assignmentJobEntity.setJobKey(jobKey);
		assignmentJobEntity.setScheduleMode(scheduleMode);
		assignmentJobEntity.setStartTime(new Timestamp(System.currentTimeMillis()));
		assignmentJobEntity.setFinishTime(new Timestamp(System.currentTimeMillis()));
		assignmentJobEntity.setStatus(JobStatusEnum.RUNNING.getValue());
		assignmentJobMapper.insertSelective(assignmentJobEntity);
		return assignmentJobEntity;
	}

	public AssignmentJobEntity getById(Long id) {
		return assignmentJobMapper.selectByPrimaryKey(id);
	}

	public List<AssignmentJobEntity> getByAssignmentId(Long assignmentId) {
		Objects.requireNonNull(assignmentId, "assignmentId不能为null");

		AssignmentJobEntity condition = new AssignmentJobEntity();
		condition.setAssignmentId(assignmentId);

		Example example = new Example(AssignmentJobEntity.class);
		example.createCriteria().andEqualTo(condition);
		example.orderBy("createTime").desc();
		return assignmentJobMapper.selectByExample(example);
	}

	public List<AssignmentJobEntity> getDistinctByAssignmentId(Long assignmentId) {
		return assignmentJobMapper.getDistinctByAssignmentId(assignmentId);
	}

	public AssignmentJobEntity getByJobName(String jobName) {
		return assignmentJobMapper.getByJobName(jobName);
	}

	public void updateSelective(AssignmentJobEntity assignmentJobEntity) {
		Objects.requireNonNull(assignmentJobEntity.getId(), "AssignmentJob的id不能为null");
		assignmentJobMapper.updateByPrimaryKeySelective(assignmentJobEntity);
	}

	public int getCountByStatus(JobStatusEnum status) {
		AssignmentJobEntity condition = new AssignmentJobEntity();
		condition.setStatus(status.getValue());

		Example example = new Example(AssignmentJobEntity.class);
		example.createCriteria().andEqualTo(condition);
		return assignmentJobMapper.selectCountByExample(example);
	}

	public int getTotalCount() {
		return Optional.ofNullable(assignmentJobMapper.selectAll())
				.orElseGet(ArrayList::new).size();
	}

	public List<OpsTaskJobTrend> queryTaskJobTrend(Integer days) {
		return assignmentJobMapper.queryTaskJobTrend(days);
	}

	public void delByAssigmentId(Long id) {
		AssignmentJobEntity entity = new AssignmentJobEntity();
		entity.setAssignmentId(id);
		assignmentJobMapper.delete(entity);
	}

}
